﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MacViet.DataBaseAPI;
using System.Globalization;
using System.Net;
using System.Net.Mail;
using System.Text;
public partial class Control_Admin_UCAccount_Registry : System.Web.UI.UserControl
{
    ACCOUNTSDataAPI api = new ACCOUNTSDataAPI();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblsSecurityCode.Text = Common.GenerateRodomCode(9, 19);
            btnUpdate.Visible = false;
        }
    }
    
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        
        DateTime dToDate = ConvertToDate(txtToDate.Text);
        DateTime dFromDate = ConvertToDate(txtFromDate.Text);
        bool payment = false;
        string securityCode = lblsSecurityCode.Text.Trim();
        if (rdlAutoPayment.SelectedValue == "1")
            payment = true;
        if (CheckEmail(txtEmail.Text.Trim()))
        {
            lblMessages.Text = "Email is exists! Do you want to use this email?";
            btnUpdate.Visible = true;
            return;
        }
        int checkDate = dFromDate.CompareTo(dToDate);
        if (checkDate == 0 || checkDate == 1)
        {
            lblMessages.Text = "ToDate greater than FromDate!";
            return;
        }
        bool chek=api.insertACCOUNTS(txtName.Text.Trim(), txtAddress.Text.Trim(), txtPhone.Text.Trim(), txtEmail.Text.Trim(), txtPassword.Text.Trim(), securityCode, dFromDate, dToDate, true, payment);
        if (chek)
            lblMessages.Text = "Register successful!";
        else
            lblMessages.Text = "Register Fail!";

    }
    public bool CheckEmail(string email)
    {
        DataTable dt = api.getAccountByEmail(email);
        if (dt != null)
            return true;
        return false;
    }
    public DateTime ConvertToDate(string date)
    {
        DateTime dt = DateTime.Now;
        if (date.Trim() != null && date.Trim() != string.Empty)
        {
            string[] strArr = date.Split('/');
            string result = strArr[1].ToString() + "/" + strArr[0].ToString() + "/" + strArr[2].ToString() + " " + DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString();
            dt = Convert.ToDateTime(result);
        }
        return dt;
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        DateTime dToDate = ConvertToDate(txtToDate.Text);
        DateTime dFromDate = ConvertToDate(txtFromDate.Text);
        string securityCode = lblsSecurityCode.Text;
        bool payment = false;
        if (rdlAutoPayment.SelectedValue == "1")
            payment = true;       
        int checkDate = dFromDate.CompareTo(dToDate);
        if (checkDate == 0 || checkDate == 1)
        {
            lblMessages.Text = "ToDate greater than FromDate!";
            return;
        }
        int accountId = 0;
        DataTable dt = api.getAccountByEmail(txtEmail.Text.Trim());
        if (dt != null)
            accountId = int.Parse(dt.Rows[0]["ACC_ID"].ToString());
        if (api.updateACCOUNTS(accountId,txtName.Text.Trim(), txtAddress.Text.Trim(), txtPhone.Text.Trim(), txtEmail.Text.Trim(), txtPassword.Text.Trim(), securityCode, dFromDate, dToDate, false, payment))        
            lblMessages.Text = "Update successful!";

    }
    public void sendMail(string email)
    {
        try
        {
            MailMessage mail = new MailMessage();
            mail.To.Add("admin@appsfactoryltd.com");            
            mail.Subject = "Registry account from appsfactoryltd.com";           
            mail.Body = @"<html><body><div style=""font-family:verdana;font-size:10pt;"">This is a test email.<a href='AccountLogin.aspx?SecurityCode='\"+email+">www.appsfactoryltd.com</a></div></body></html>";
            mail.BodyEncoding = Encoding.UTF8;
            mail.IsBodyHtml = true;
            mail.From = new MailAddress(email);
            SmtpClient client = new SmtpClient();
            client.Host = "mail.appsfactoryltd.com";
            client.Port = 1025;
            client.Send(mail);
            Label1.Text = "sucessful!";
        }
        catch (System.Net.Mail.SmtpException ex)
        {
           return;
        }
    }
 }

